package com.android.hidephotosplus;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import uk.co.senab.photoview.HackyViewPager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.actionbarsherlock.app.SherlockActivity;
import com.android.hidephotosplus.adapter.GalleryAdapter;
import com.android.hidephotosplus.adapter.ViewMediaAdapter;
import com.android.hidephotosplus.interfaces.MInterface.onTask;
import com.android.hidephotosplus.model.ModelMedia;
import com.android.hidephotosplus.util.Config;
import com.android.hidephotosplus.util.PasswordManager;
import com.android.hidephotosplus.util.Util;

public class ActivityViewMedia extends SherlockActivity implements
		OnPageChangeListener, OnItemClickListener, OnClickListener {

	private HackyViewPager mViewPager;

	private ArrayList<ModelMedia> arrMedias;

	private ViewMediaAdapter mViewPhotoAdapter;

	@SuppressWarnings("deprecation")
	private Gallery mGallery;

	private GalleryAdapter mGalleryAdapter;

	private boolean isPublic;

	private int position = 0;

	private LinearLayout mLinearActionMode;

	private ImageView imageHide;

	private ImageView imageUnHide;

	private ImageView imageSlideShow;

	private ImageView imageDelete;

	private ImageView imageShare;

	private ImageView imageBack;

	private ImageView imageHome;

	private ArrayList<ModelMedia> arrMediasSelected = new ArrayList<ModelMedia>();

	private Timer mTimer = new Timer();

	public static boolean IS_START_PASSWORD = false;

	public PasswordManager mPasswordManager;

	private String navigationSelected = null;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewphoto);
		mViewPager = (HackyViewPager) findViewById(R.id.pager);
		mGallery = (Gallery) findViewById(R.id.gallery);
		mLinearActionMode = (LinearLayout) findViewById(R.id.actionmode);

		mPasswordManager = new PasswordManager(this);
		IS_START_PASSWORD = false;

		imageHide = (ImageView) findViewById(R.id.imageHide);
		imageUnHide = (ImageView) findViewById(R.id.imageUnHide);
		imageDelete = (ImageView) findViewById(R.id.imageDelete);
		imageSlideShow = (ImageView) findViewById(R.id.imageSlideShow);
		imageShare = (ImageView) findViewById(R.id.imageShare);
		imageBack = (ImageView) findViewById(R.id.imageBack);
		imageHome = (ImageView) findViewById(R.id.imageHome);
		imageHide.setOnClickListener(this);
		imageUnHide.setOnClickListener(this);
		imageDelete.setOnClickListener(this);
		imageSlideShow.setOnClickListener(this);
		imageShare.setOnClickListener(this);
		imageBack.setOnClickListener(this);
		imageHome.setOnClickListener(this);

		Intent intent = getIntent();
		if (intent.getExtras() != null) {
			isPublic = intent.getExtras().getBoolean(Config.KEY_IS_PUBLIC);
			navigationSelected = intent.getExtras().getString(
					Config.KEY_NAVIGATION_SELECTED);
			arrMedias = intent
					.getParcelableArrayListExtra(Config.KEY_ARR_MEDIA);

			position = intent.getExtras().getInt(Config.KEY_POSITION_ARR_MEDIA);
			mViewPager.setOnPageChangeListener(this);
			mGalleryAdapter = new GalleryAdapter(this, arrMedias, isPublic);
			mGallery.setAdapter(mGalleryAdapter);
			mGallery.setOnItemClickListener(this);
			mViewPhotoAdapter = new ViewMediaAdapter(this, arrMedias, mGallery,
					isPublic, mLinearActionMode, mTimer);
			mViewPager.setAdapter(mViewPhotoAdapter);
			mViewPager.setCurrentItem(position);
			mGallery.setSelection(position);
			if (isPublic) {
				imageHide.setVisibility(View.VISIBLE);
				imageUnHide.setVisibility(View.GONE);
			} else {
				imageHide.setVisibility(View.GONE);
				imageUnHide.setVisibility(View.VISIBLE);
			}
			// Start slide
			if (intent.hasExtra(Config.KEY_SLIDE_SHOW)) {
				startSlideShow();
			}
		}

	}

	@Override
	public void onPause() {
		super.onPause();
		IS_START_PASSWORD = true;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (IS_START_PASSWORD) {
			mPasswordManager.checkSetupOrUnlockPass();
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int position, float arg1, int arg2) {
		arrMediasSelected = new ArrayList<ModelMedia>();
		arrMediasSelected.add(arrMedias.get(position));
	}

	@Override
	public void onPageSelected(int arg0) {
		mGallery.setSelection(mViewPager.getCurrentItem());
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		mViewPager.setCurrentItem(position);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {

		case R.id.imageBack:
			finish();
			break;
		case R.id.imageHome:
			finish();
			break;
		case R.id.imageHide:
			new Util.HiddenMedia(this, arrMediasSelected, null, null, true,
					new onTask() {
						@Override
						public void onTaskComplete(Object object) {
							sendBroastcastArrMedia();
							ActivityViewMedia.this.arrMedias
									.removeAll(arrMediasSelected);
							arrMediasSelected.clear();
							mViewPhotoAdapter.notifyDataSetChanged();
							mGalleryAdapter.notifyDataSetChanged();
						}
					}).execute();

			break;
		case R.id.imageUnHide:
			new Util.UnHideMedia(this, arrMediasSelected, null, null, true,
					new onTask() {
						@Override
						public void onTaskComplete(Object object) {
							sendBroastcastArrMedia();
							ActivityViewMedia.this.arrMedias
									.removeAll(arrMediasSelected);
							arrMediasSelected.clear();
							mViewPhotoAdapter.notifyDataSetChanged();
							mGalleryAdapter.notifyDataSetChanged();
						}
					}).execute();

		case R.id.imageSlideShow:
			startSlideShow();
			break;

		case R.id.imageDelete:
			if (isPublic) {
				new Util.DeleteMediaStock(this, arrMediasSelected, null, null,
						true, new onTask() {
							@Override
							public void onTaskComplete(Object object) {
								sendBroastcastArrMedia();
								ActivityViewMedia.this.arrMedias
										.removeAll(arrMediasSelected);
								arrMediasSelected.clear();
								mViewPhotoAdapter.notifyDataSetChanged();
								mGalleryAdapter.notifyDataSetChanged();
							}
						}).execute();
			} else {
				new Util.DeleteMediaHidden(this, arrMediasSelected, null, null,
						true, new onTask() {
							@Override
							public void onTaskComplete(Object object) {
								sendBroastcastArrMedia();
								ActivityViewMedia.this.arrMedias
										.removeAll(arrMediasSelected);
								arrMediasSelected.clear();
								mViewPhotoAdapter.notifyDataSetChanged();
								mGalleryAdapter.notifyDataSetChanged();
							}
						}).execute();
			}

			break;
		case R.id.imageShare:
			if (isPublic)
				new Util.ShareFileStock(ActivityViewMedia.this, null,
						arrMediasSelected, navigationSelected).execute();
			else
				new Util.ShareFileHidden(ActivityViewMedia.this, null,
						arrMediasSelected, navigationSelected).execute();
			break;
		default:
			break;
		}

	}

	private void sendBroastcastArrMedia() {
		Intent intent = new Intent();
		intent.setAction(Config.ACTION_SEND_POSITON_HIDE_DELETE);
		intent.putExtra(Config.KEY_SEND_POSITON_HIDE_DELETE,
				mViewPager.getCurrentItem());
		sendBroadcast(intent);
	}

	private void startSlideShow() {
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		final Handler mHandler = new Handler();
		final Runnable mRunnable = new Runnable() {
			@Override
			public void run() {
				if (position >= 0 && position < arrMedias.size())
					mViewPager.setCurrentItem(position++, false);
				else
					mTimer.cancel();
			}
		};
		int delay = 1000; // delay for 1 sec.
		// TODO Time slide
		int period = 4 * 1000; // repeat every sec.
		mTimer = new Timer();
		mTimer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				mHandler.post(mRunnable);
			}
		}, delay, period);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case Config.REQUEST_CODE_START_PATTERN_PASSWORD:
			if (resultCode == Activity.RESULT_OK) {
				IS_START_PASSWORD = false;
			}
			break;
		default:
			break;
		}
	}
}
